package com.example.service.impl;

import com.example.dao.PersonMapper;
import com.example.dto.PersonDTO;
import com.example.entity.Person;
import com.example.service.PersonService;
import com.example.vo.PersonOfCarVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author taoshen
 * @create 2022/1/23
 */
@Service
public class PersonServiceImpl implements PersonService {
    @Autowired
    private PersonMapper personMapper;

    @Override
    public List<Person> findAll() {
        return personMapper.findAll();
    }

    @Override
    public PersonOfCarVO findOnePersonCarBrands(String name) {
        List<String> carBrandList = personMapper.findOnePersonCarBrands(name);

        PersonOfCarVO personOfCarVO = new PersonOfCarVO();
        personOfCarVO.setName(name);
        personOfCarVO.setCarBrands(carBrandList);
        return personOfCarVO;
    }

    @Override
    public void insertOnePerson(PersonDTO personDTO) {
        // nameAge 是有的 但是name和age没有
        String nameAge = personDTO.getNameAge();
        // zhan:wuji : 17
        personDTO.setName(nameAge.substring(0, nameAge.lastIndexOf(":")));
        personDTO.setAge(Integer.parseInt(nameAge.substring(nameAge.lastIndexOf(":") + 1)));

        personMapper.insertOnePerson(personDTO);
    }
}
